/*
*
*	The Gimme ECMAScript Library by Stephen Stchur
*	"Taking the pain out of Javascript!"
*
*	Copyright (c) Microsoft Corporation.  All rights reserved.
*
*	Description:
*	Gimme AJAX Module -- Helper methods designed to ease management of asynchronous requests in Javascript
*
*/

Gimme.AJAX = new function()
{
	this.requestJSON = function(_uri, _callback)
	{
		DoAsyncRequest(_uri, _callback, true);
	};

	this.requestAHAH = function(_uri, _callback)
	{
		DoAsyncRequest(_uri, _callback, false);
	};

	function CreateXHR()
	{
		if (typeof XMLHttpRequest !== 'undefined')
		{
			return new XMLHttpRequest();
		}
		else if (typeof ActiveXObject !== 'undefined')
		{
			try { return new ActiveXObject('Microsoft.XMLHTTP'); }
			catch(everything) { throw new Error('Error invoking XMLHTTP'); }
		}
		else
		{
			throw new Error('XMLHttp is not supported');
		}
	}

	// XHR Stuff
	function DoAsyncRequest(_uri, _callback, _isJSON)
	{
		var xhr = CreateXHR();
		xhr.onreadystatechange = function()
		{
			if (xhr.readyState === 4)
			{
				if (xhr.status === 200)
				{
					var data = xhr.responseText;
					if (_isJSON) { data = eval('(' + xhr.responseText + ')'); }
					_callback.call(xhr, data);
				}

				xhr = null;
				callback = null;
			}
		}
		xhr.open('GET', _uri, true);
		xhr.send('');
	}
};

